/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DA;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Guigo
 */
public class DARoles {

    String conexionString = "jdbc:sqlserver://localhost:1433;"
            + "databaseName=Viaticos;user=sa; password=admin;";

    Connection con = null;

    public ResultSet getAllRoles() throws SQLException {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(conexionString);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DAFuncionarios.class.getName()).log(Level.SEVERE, null, ex);
        }
        String query = "SELECT * FROM Roles";
        Statement stmnt = con.createStatement();
        return stmnt.executeQuery(query);
    }

    public int getRol(String nombreRol) throws SQLException {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(conexionString);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DAFuncionarios.class.getName()).log(Level.SEVERE, null, ex);
        }
        String query = "SELECT idRol FROM Roles WHERE "
                + "DescripcionRol LIKE '%" + nombreRol + "%'";
        Statement stmnt = con.createStatement();
        ResultSet rs = stmnt.executeQuery(query);

        if (rs.next()) {
            return rs.getInt("idRol");
        } else {
            return -1;
        }
    }
}
